package cc.idoone.spring_boot_hello.h2;

import cc.idoone.spring_boot_hello.h2.jpa.JpaStaffDao;
import cc.idoone.spring_boot_hello.h2.jpa.StaffBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @auther hemj
 * @since 2017/9/24
 */
@Service
@Transactional(transactionManager = "h2JpaManager")
public class JpaStaffServiceImpl implements StaffService {

	@Autowired
	private JpaStaffDao jpaStaffDao;

	@Override
	public void createTable() {
	}

	@Override
	public void save() {
		StaffBo staffBo = new StaffBo();
		staffBo.setUserId("zs");
		staffBo.setUserName("张三");
		staffBo.setAge(10);
		jpaStaffDao.save(staffBo);

		staffBo = new StaffBo();
		staffBo.setUserId("ls");
		staffBo.setUserName("李四");
		staffBo.setAge(15);
		jpaStaffDao.save(staffBo);

		staffBo = new StaffBo();
		staffBo.setUserId("ww");
		staffBo.setUserName("王五");
		staffBo.setAge(20);
		jpaStaffDao.save(staffBo);

		staffBo = new StaffBo();
		staffBo.setUserId("ABC");
		staffBo.setUserName("ABC");
		staffBo.setAge(21);
		jpaStaffDao.save(staffBo);

		staffBo = new StaffBo();
		staffBo.setUserId("Abc");
		staffBo.setUserName("Abc");
		staffBo.setAge(22);
		jpaStaffDao.save(staffBo);

		staffBo = new StaffBo();
		staffBo.setUserId("abC");
		staffBo.setUserName("abC");
		staffBo.setAge(23);
		jpaStaffDao.save(staffBo);
	}

	@Transactional(transactionManager = "h2JpaManager", propagation = Propagation.SUPPORTS)
	@Override
	public List<StaffBo> findAll() {
		return (List<StaffBo>) jpaStaffDao.findAll();
	}

	@Transactional(transactionManager = "h2JpaManager", propagation = Propagation.SUPPORTS)
	@Override
	public StaffBo findByName() {
		return jpaStaffDao.findByUserName("张三");
	}

	@Transactional(transactionManager = "h2JpaManager", propagation = Propagation.SUPPORTS)
	@Override
	public List<StaffBo> findByUserNameIgnoreCase(String name) {
		return jpaStaffDao.findByUserNameIgnoreCase(name);
	}

	@Transactional(transactionManager = "h2JpaManager", propagation = Propagation.SUPPORTS)
	@Override
	public List<StaffBo> findByUserIdAndUserNameAllIgnoreCase(String id, String name) {
		return jpaStaffDao.findByUserIdAndUserNameAllIgnoreCase(id, name);
	}

	@Transactional(transactionManager = "h2JpaManager", propagation = Propagation.SUPPORTS)
	@Override
	public List<StaffBo> getByMinAge(int minAge) {
		return jpaStaffDao.getByMinAge(minAge);
	}

	@Transactional(transactionManager = "h2JpaManager", propagation = Propagation.SUPPORTS)
	@Override
	public StaffBo findByUserId(String id) {
		return jpaStaffDao.findByUserId(id);
	}

	@Transactional(transactionManager = "h2JpaManager", propagation = Propagation.SUPPORTS)
	@Override
	public Page<StaffBo> findByAgeGreaterThan(Integer age, Pageable pageable) {
		return jpaStaffDao.findByAgeGreaterThan(age, pageable);
	}

	@Override
	public int increaseAge(int after, int before) {
		return jpaStaffDao.increaseAge(after, before);
	}
}
